#include "mainwindow.h"
#include "ui_mainwindow.h"
//#include <gloox/client.h>

#include <QDebug>

using namespace std;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    logWindow = new LogWindow(this);
    connect(Logger::Instance(), SIGNAL(entryAdded(QString)), logWindow, SLOT(addLogEntry(QString)));
    qInstallMsgHandler(logHandler);
    createContactList();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    JID jid;
    jid.setServer("jabber.ru");
    jid.setUsername("denzeldev");

    jid.setResource("test");
    // Кто поменяет тот дурак :)
    XMPPClient *client = new XMPPClient(jid, "6cpbbAejDE");
    client->attach(new XMPPMessaging());
    XMPPRoster *xmppRoster = new XMPPRoster();
    client->attach(xmppRoster);
    localRoster.addRosterManager(client->rosterManager());
    localRoster.addRoster(xmppRoster);

    accountManager.addAccount(client);
    client->connect();

    logWindow->show();
}

// disconnect // TODO: TEMP!
void MainWindow::on_pushButton_2_clicked()
{
    accountManager.disconnectAll();
}

void MainWindow::logHandler(QtMsgType type, const char *msg)
{
    if (type != QtDebugMsg)
        return;
    Logger::Instance()->addLogEntry(QString(msg));
}

// Menu->Debug log
void MainWindow::on_actionDebug_log_triggered()
{
    logWindow->show();
}

void MainWindow::on_actionAbout_triggered()
{
    AboutWindow *about = new AboutWindow(this);
    about->setWindowModality(Qt::WindowModal);
    about->show();
}


void MainWindow::createContactList()
{
    contactList = new ContacttListWidget(this);
    ui->contactListLayout->addWidget(contactList);
    contactList->setColumnCount(3);
    contactList->setHeaderHidden(true);

    // Connecting to local roster
    connect(&localRoster, SIGNAL(rosterRecieved(Roster,JID)), contactList, SLOT(rosterRecieved(Roster,JID)));
}

// FOR TEST PURPOSES ONLY!
void MainWindow::on_testButton_clicked()
{
}
